/**
 * @file LoginCommand.cs
 * @author Zane Mayo
 * @date 06/05/2009
 */

using System;

namespace MxitAPI {
	
	/**
	 * Populates a login message and sends it to the Mxit server.
	 * Updates the model to show whether the user has logged in successfully.
	 */
	public class LoginHandler : Handler {
		
		private static log4net.ILog log = log4net.LogManager.GetLogger( System.Reflection.MethodBase.GetCurrentMethod().DeclaringType );
		
		private string loginName;
		private string password;
		//public string EncryptedPassword { get; set; }
		
		public LoginHandler() {
			init();
		}
		
		public LoginHandler( string loginName, string password ) : base( typeof( LoginProtocolMessage ) ) {
			init( loginName, password );
		}
		
		public void init( string loginName, string password ) {
			this.loginName = loginName;
			this.password = password;
		}
		
		public override ProtocolMessage CreateRequestProtocolMessage() {
			
			Connection connection = Connection.Instance;
			ApplicationInfo application = ApplicationInfo.Instance;
			LoginProtocolMessage loginMessage = new LoginProtocolMessage();
			connection.LoginName = loginName;
			loginMessage.Password = Utilities.EncryptPassword( password );
			//loginMessage.Password = password;
			loginMessage.Version = application.Version;
			loginMessage.GetContacts = 0;
			loginMessage.Capabilities = application.Capabilities;
			loginMessage.DistributionCode = application.DistributionCode;
			loginMessage.Features = application.Features;
			loginMessage.Dialingcode = application.DialingCode;
			loginMessage.Locale = application.Locale;
			
			return loginMessage;
		}
		
		protected override void HandleSpecificResponseProtocolMessage( ProtocolMessage message ) {
			
			LoginProtocolMessage loginMessage = message as LoginProtocolMessage;
			if (loginMessage == null) 
				throw new MxitException( "Could not handle response. The message is of the wrong type." );
			
			Connection connection = Connection.Instance;
			ApplicationInfo application = ApplicationInfo.Instance;
			
			if ( loginMessage.ErrorCode == ErrorCode.NoError ) {
				// Todo set this through modelFacade?
				
				connection.Password = password; 
				connection.LoginName = loginMessage.ReturnedLoginName;
				connection.LoginDate = loginMessage.Date;
				connection.CurrentUrl = loginMessage.Url;
				application.MaxSupportedVersion = loginMessage.MaxSupportedVersion;
				//application.PricePlan = loginMessage.PricePlan;
				string flags = loginMessage.Flags;
				log.Debug( "Poll Data" );
				log.Debug( loginMessage.PollData );
				//Console.WriteLine( "Login Flags: " + flags );
				//foreach ( char a in loginMessage.PollData ) {
				//	Console.WriteLine( a );
				//}
				//Console.WriteLine( "Login poll data: " + loginMessage.PollData );
				if ( connection.PrimaryServer is HttpServerInfo) {
				//if ( connection.PrimaryServer.GetType() == typeof( HttpServerInfo ) ) {
					(( HttpServerInfo ) connection.PrimaryServer).SessionID = loginMessage.SessionId;
					log.Debug( "SessionID set for this http connection" );
					log.Debug( (( HttpServerInfo ) connection.PrimaryServer).SessionID );
				}
					
				connection.ConnectionStatus = ConnectionStatus.Connected;
					
				// Todo handle poll data
			}
			else if ( loginMessage.ErrorCode == ErrorCode.InvalidRegistrationOrPassword ) {
			//	Console.WriteLine( "Login Failed" );
			}
            connection.DoLoginEvent();
		}


	}
}
